package top.jfunc.network.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiongshiyan at 2019/5/7 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@RestController
@RequestMapping("/cookie")
public class CookieController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(CookieController.class);

    @GetMapping("getCookie")
    public String getCookie(HttpServletResponse response){
        Cookie cookie = new Cookie("name", "value");
        Cookie cookie1 = new Cookie("etag" , "safhdsfajsfd");
        Cookie cookie2 = new Cookie("dasda" , "safhddsasdasfajsfd");
        Cookie cookie3 = new Cookie("name" , "value2");
        response.addCookie(cookie);
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        response.addCookie(cookie3);
        return "success";
    }
    @GetMapping("setCookie")
    public String setCookie(HttpServletRequest httpServletRequest){
        System.out.println("-------------------");
        Cookie[] cookies = httpServletRequest.getCookies();
        printHeaders(logger , httpServletRequest);
        if(null != cookies && cookies.length>0){
            for (Cookie cookie : cookies) {
                logger.info(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "success";
    }
}
